package com.sise.composition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sise.entity.ProductInfo;
import com.sise.entity.Rating;
import com.sise.entity.User;
import com.sise.mapper.ProductInfoMapper;
import com.sise.mapper.RatingMapper;
import com.sise.mapper.UserMapper;
import com.sise.recomder.LibrecRecomder;
import lombok.extern.slf4j.Slf4j;
import net.librec.recommender.item.RecommendedItem;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @author zj
 */
@Slf4j
@Component
public class JobsTest {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RatingMapper ratingMapper;

    @Resource
    private LibrecRecomder librecRecomder;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Scheduled(cron = "0 0 0 * * * ")
    public void generateRating() {
        //随机生成评分
        List<User> userList = userMapper.selectList(null);
        List<ProductInfo> productInfoList = productInfoMapper.selectList(null);
        for (User user : userList) {
            for (ProductInfo productInfo : productInfoList) {
                QueryWrapper<Rating> ratingQueryWrapper = new QueryWrapper<>();
                ratingQueryWrapper.lambda().eq(Rating::getUser, user.getOpenId())
                        .eq(Rating::getItem, productInfo.getProductId());
                Rating rating = ratingMapper.selectOne(ratingQueryWrapper);
                Double randomRating = (double) new Random().nextInt(11) / 2;
                if (rating != null) {
                    rating.setItemRating(String.valueOf(randomRating));
                    ratingMapper.update(rating, ratingQueryWrapper);
                } else {
                    ratingMapper.insert(new Rating().setUser(user.getOpenId())
                            .setItem(String.valueOf(productInfo.getProductId()))
                            .setItemRating(String.valueOf(randomRating))
                            .setItemValue("0"));
                }
            }
        }

        //将评分导出到txt
        List<Rating> ratingList = ratingMapper.selectList(null);
        File file;
        try {
            file = ResourceUtils.getFile("classpath:data/train/data_set.txt");
            if (!file.exists()) {
                if (file.createNewFile()) {
                    log.info("创建文件成功");
                } else {
                    log.info("创建文件失败");
                }
            }
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                for (Rating rating : ratingList) {
                    bw.write(rating.getUser() + " " +
                            rating.getItem() + " " +
                            rating.getItemRating() + "\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //将推荐值写到数据库
        List<RecommendedItem> mfals = librecRecomder.mfals();
        for (RecommendedItem recommendedItem : mfals) {
            QueryWrapper<Rating> ratingQueryWrapper = new QueryWrapper<>();
            ratingQueryWrapper.lambda().eq(Rating::getUser, recommendedItem.getUserId())
                    .eq(Rating::getItem, recommendedItem.getItemId());
            Rating rating = ratingMapper.selectOne(ratingQueryWrapper);
            rating.setItemValue(String.valueOf(recommendedItem.getValue()));
            ratingMapper.update(rating, ratingQueryWrapper);
        }
    }
}
